package com.franklin.java.action.move;

import com.intellij.ide.actions.GotoActionBase;
import com.intellij.ide.util.gotoByName.ChooseByNamePopup;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

/**
 * @author Franklin
 * @since 2021/9/17 17:32
 */
public class MyGotoTargetAction extends GotoActionBase {

    protected PsiElement targetElement;

    public MyGotoTargetAction(PsiElement targetElement) {
        this.targetElement = targetElement;
    }

    @Override
    protected void gotoActionPerformed(@NotNull AnActionEvent e) {
        final Project project = e.getData(CommonDataKeys.PROJECT);
        if (project == null) return;
        MyGotoTargetModel myGotoTargetModel = new MyGotoTargetModel(project);
        showNavigationPopup(e,myGotoTargetModel, new GotoActionCallback<>() {
            @Override
            public void elementChosen(ChooseByNamePopup popup, Object element) {
                if (targetElement instanceof Navigatable){
                    ((Navigatable) targetElement).navigate(true);
                }
            }
        },false);
    }
}
